﻿using System;
using System.Collections.Concurrent;
using log4net;

namespace Hjt212Parser
{
    public class MyTimer
    {
        private static readonly ConcurrentDictionary<string, DateTime> AllTimers = new ConcurrentDictionary<string, DateTime>();
        private static readonly ILog Log = LogManager.GetLogger(typeof(MyTimer));
        /// <summary>
        /// 特制定时器，专门为信息缓冲使用。
        /// </summary>
        /// <param name="methodName">一般为方法名称</param>
        /// <param name="spanMinute">间隔分钟</param>
        /// <returns></returns>
        public static bool IsToTheTime(string methodName, int spanMinute)
        {
            try
            {
                var now = DateTime.Now;
                if (!AllTimers.ContainsKey(methodName))
                {
                    AllTimers.TryAdd(methodName, now);
                    return false;
                }

                AllTimers.TryGetValue(methodName, out var before);
                if ((now - before).TotalMinutes > spanMinute)
                {
                    AllTimers.TryRemove(methodName, out before);
                    AllTimers.TryAdd(methodName, now);
                    return true;
                }

            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            return false;
        }

        public static bool IsToTheTime(string methodName, int spanMinute,bool firstTimeReturnValue)
        {
            try
            {
                var now = DateTime.Now;
                if (!AllTimers.ContainsKey(methodName))
                {
                    AllTimers.TryAdd(methodName, now);
                    return firstTimeReturnValue;
                }

                AllTimers.TryGetValue(methodName, out var before);
                if ((now - before).TotalMinutes > spanMinute)
                {
                    AllTimers.TryRemove(methodName, out before);
                    AllTimers.TryAdd(methodName, now);
                    return true;
                }

            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            return false;
        }

        public static void UpdateToNow(string methodName)
        {
            try
            {
                var now = DateTime.Now;
                if (!AllTimers.ContainsKey(methodName))
                {
                    AllTimers.TryAdd(methodName, now);
                }
                else
                {
                    AllTimers.TryRemove(methodName, out _);
                    AllTimers.TryAdd(methodName, now);
                } 
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

        public static void Remove(string methodName)
        {
            try
            {               
                if (AllTimers.ContainsKey(methodName))
                {
                    AllTimers.TryRemove(methodName, out _);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }

    }
}
